Fastify is a small Node framework for developing back end web apps.
In this article, we’ll look at how to create back end apps with Fastify.
Async Await
We can use async and await in our route handlers.
For example, we can write:
const fastify = require('fastify')()
const getData = () => Promise.resolve('foo')
const processData = (data) => Promise.resolve(data)
const opts = {
schema: {
response: {
200: {
type: 'string'
}
}
}
}
fastify.get('/', opts, async function (request, reply) {
const data = await getData()
const processed = await processData(data)
return processed
})
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
We use an async function as the route handler.
then we can use await in the handler to run promises.
We an also use reply.send to send back the data.
For example, we can write:
const fastify = require('fastify')()
const getData = () => Promise.resolve('foo')
const processData = (data) => Promise.resolve(data)
const opts = {
schema: {
response: {
200: {
type: 'string'
}
}
}
}
fastify.get('/', opts, async function (request, reply) {
const data = await getData()
const processed = await processData(data)
reply.send(processed)
})
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
to call reply.send to send back the response.
We can also await our reply if we’re calling reply.send in an async callback.
For example, we can write:
const fastify = require('fastify')()
const getData = () => Promise.resolve('foo')
const processData = (data) => Promise.resolve(data)
const opts = {
schema: {
response: {
200: {
type: 'object',
properties: {
hello: {
type: 'string'
}
}
}
}
}
}
fastify.get('/', opts, async function (request, reply) {
setImmediate(() => {
reply.send({ hello: 'world' })
})
await reply
})
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
to await our reply since we called reply.send in the setImmediate callback.
When using both return value and reply.send(value) at the same time, the first one that happens takes precedence.
If we use async and await with reply.send , then we shouldn’t return any value.
And if we’re using async and await with promises, then we should return the value and don’t use reply.send or return undefined .
Route Prefixing
We can add prefixes to routes.
For example, we can write:
index.js
const fastify = require('fastify')()
fastify.register(require('./routes/v1/users'), { prefix: '/v1' })
fastify.register(require('./routes/v2/users'), { prefix: '/v2' })
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
routes/v1/users.js
module.exports = function (fastify, opts, done) {
fastify.get('/user', async () => 'v1')
done()
}
routes/v2/users.js
module.exports = function (fastify, opts, done) {
fastify.get('/user', async () => 'v2')
done()
}
Then when we go to /v1/user, we see v1 .
And when we go to /v2/user, we see v2 .
Conclusion
We can add async functions as route handlers.
And we can add route prefixes with Fastify.